'use strict';

// todoPage controller
app.controller('power', ['$state','$filter','$window','$http','$scope','$modal','toaster','$log', function($state,$filter,$window,$http,$scope,$modal,toaster,$log) {
    $scope.title = {
        title:'用户权限列表',
        addTitle:'用户权限信息'
    };
    $scope.toaster = {
        type: 'warning',
        title: '提示：',
        text: '内容不能为空！'
    };

    $scope.pop = function(){
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    };
    function showMessage(type,title,text){
        $scope.toaster.type = type;
        $scope.toaster.text = text;
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    }
    function checkItems(){
        var items = ['powerUserName'];
        var itemsValue = ['用户姓名'];
        for(var i=0;i<items.length;i++){
            if($('#'+items[i]+'').val()===''){
                $('#'+items[i]+'').focus();
                showMessage('warning','',itemsValue[i]+"不能为空！");
                return false;
            }
        }
        return true;
    }
    function addDo(){
        $scope.row.createTime = new Date();
        var info="";
        toaster.postInfo('/a_yongan/add/power',$scope.row).then(function(data){
            if(data && data.data && data.data.success){
                showMessage('seccess','','保存成功！');
                $scope.warehouseAdd = !($scope.warehouseAdd);
                $scope.warehouseList = !($scope.warehouseList);
                getList();
                return info;
            }else{
                if(data.data.error!==""){
                    showMessage('warning','',data.data.error);
                }else{
                    showMessage('warning','',"保存失败！");
                }
                info = "请求错误！";
            }
        });
        return info;
    }
    function getList(){
        toaster.getInfo('/a_yongan/list/power',null).then(function(data){
            if(data && data.data && data.data.data){
                makeData(data.data.data);
            }
        });
    }
    getList();
    function makeData(data){
        data.forEach(function (item) {
            item.createTime = $filter("date")(item.createTime, "yyyy/MM/dd");
        })
        $scope.saveDate = data;
    }
    $scope.message = '是否确定保存？';
    $scope.saveWarehouse = function (size) {
        if(!checkItems()){
            return;
        }
        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: 'ModalInstanceCtrl',
            size: size,
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            var info = addDo();

            //$state.reload('app/warehouse');
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.isshow=true;
    $scope.warehouseAdd = false;
    $scope.warehouseList = true;
    $scope.warehouseJump = function(ts) {
        if(ts!=='back'){
            $scope.row = {
                'powerId':"",
                'createTime':$filter("date")(new Date(), "yyyy/MM/dd"),
                'zdr':$.cookie('userName'),
                'zdrId':$.cookie('userId'),
                'powerPrice':false,
                'powerSaleOrder':false,
                'powerSaleWarehousing':false,
                'powerSaleReturn':false,
                'powerPurchasingOrder':false,
                'powerPurchasingWarehousing':false,
                'powerPurchasingReturn':false,
                'powerOthergoodOut':false,
                'powerOthergoodIn':false,
                'powerTransfer':false,
                'powerProductionPlan':false,
                'powerProcessProcess':false,
                'powerInventory':false,
                'powerCommodityAssembly':false,
                'powerMerchandiseDisassembly':false,
                'powerProductMix':false,
                'powerFlow':false,
                'powerUser':false,
                'powerDept':false,
                'powerPower':false,
                'powerCustomer':false,
                'powerSelect':false,
                'powerWarehouse':false,
                'powerGoodsType':false,
                'powerGoods':false,
                'powerInitialEntry':false,
                'powerNumber':'PW'+$filter("date")(new Date(), "yyyyMMddHHmmss")
            };
        }else{
            getList();
        }
        $scope.warehouseAdd = !($scope.warehouseAdd);
        $scope.warehouseList = !($scope.warehouseList);
    };
    $scope.row = {};
    $scope.columns = [
        {
            title:'创建时间',
            show:true
        },
        {
            title:'用户姓名',
            show:true
        },
        {
            title:'用户ID',
            show:true
        },
        {
            title:'文件编码',
            show:true
        }
    ];
    $scope.showSelectUser = function(id){
        var modalInstance = $modal.open({
            templateUrl: 'selectUser.html',
            controller: 'selectUserCtrl',
            size: 'sm',
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            $scope.row.powerUserName = selectedItem.name;
            $scope.row.powerUserId = selectedItem.eName;
            showMessage('success','','选择成功！');
            //$scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.saveDate=[];
    $scope.showInfo = function(row){
        $scope.warehouseAdd = !($scope.warehouseAdd);
        $scope.warehouseList = !($scope.warehouseList);
        $scope.row = row;
    }
}])
;